import {
  disable as darkreaderDisable,
  enable as darkreaderEnable,
  setFetchMethod as setFetch,
} from '@umijs/ssr-darkreader';

const updateDarkTheme = async (dark = false) => {
  if (typeof window === 'undefined') return;
  if (typeof window.MutationObserver === 'undefined') return;

  if (dark) {
    const defaultTheme = {
      brightness: 100,
      contrast: 90,
      sepia: 10,
    };

    const defaultFixes = {
      invert: [],
      css: '',
      ignoreInlineStyle: ['.react-switch-handle'],
      ignoreImageAnalysis: [],
      disableStyleSheetsProxy: true,
    };
    if (window.MutationObserver && window.fetch) {
      setFetch(window.fetch);
      darkreaderEnable(defaultTheme, defaultFixes);
    }
  } else {
    if (window.MutationObserver) darkreaderDisable();
  }
};
export default updateDarkTheme;